import java.security.KeyStore.Entry;
import java.util.HashMap;
import java.util.Map;

/*
 * @lc app=leetcode.cn id=387 lang=java
 *
 * [387] 字符串中的第一个唯一字符
 */

// @lc code=start
class Solution {
    public int firstUniqChar(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if(map.containsKey(s.charAt(i))){
                map.put(s.charAt(i), -1);
            }else{
                map.put(s.charAt(i), i);
            }
        }
        int first_index = s.length();
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            int pos = entry.getValue();
            if(pos != -1 && pos < first_index){
                first_index = pos;
            }
        }
        return first_index == s.length() ? -1 : first_index;
    }
}
// @lc code=end

